import processing.core.*; 
import processing.xml.*;


import ddf.minim.*; 
import ddf.minim.analysis.*; 
import ddf.minim.signals.*; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class mathemusics extends PApplet {

/**
Shows the frequency spectrum of line-in
 */





Minim minim;
AudioInput in;
AudioOutput out;
AudioRecorder recorder;
FFT fft;
FFT referencefft;
SineWave sine;

public void setup()
{
  size(800, 400, P2D);
  textMode(SCREEN); 
  
  minim = new Minim(this);

  // get a stereo line-in: sample buffer length of 2048
  // default sample rate is 44100, default bit depth is 16
  in = minim.getLineIn(Minim.STEREO, 2048);
  out = minim.getLineOut(Minim.STEREO,2048);
  // create a recorder that  will record from the input to the filename specified, using buffered recording
  // buffered recording means that all captured audio will be written into a sample buffer
  // then when save() is called, the contents of the buffer will actually be written to a file
  // the file will be located in the sketch's root folder.
  recorder = minim.createRecorder(in, "myrecording.wav", true);
  fft = new FFT(in.bufferSize(), in.sampleRate()); 
  referencefft = new FFT(in.bufferSize(), in.sampleRate()); 
  fft.logAverages(22, 32);
  referencefft.logAverages(22, 32);
  sine = new SineWave(220, 2, out.sampleRate());
//  out.addSignal(sine);
  textFont(createFont("SanSerif", 12));
  rectMode(CORNERS);
}

public void draw()
{
  background(0); 
  stroke(255);
  // draw the waveforms

  fft.forward(in.mix);
  referencefft.forward(out.mix);
  int w = PApplet.parseInt(width/fft.avgSize());
  for(int i = 16; i < fft.avgSize(); i++)
  {
    // draw a rectangle for each average, multiply the value by 2 so we can see it better
    stroke(255,0,0);
    fill(255,0,0);
    float sig = 14*fft.getAvg(i);
    if(sig<30)sig=0;
//    else sig = 200;
    rect(i*w, height/2-30, i*w + w, height/2-30 - sig);
    if(i%32==0){
      stroke(255);
      fill(255);
      rect(i*w, height/2-30, i*w +1, 0);
      text((int)(i/1024.0f*44100),i*w,height/2);
    }
    stroke(180);
    if(i%4==0){
      rect(i*w, height/2-30, i*w, 0);
    }

  }
 

  fft.forward(in.mix);
  referencefft.forward(out.mix);
  for(int i = 0; i < fft.specSize(); i++){
    // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
    float sig = fft.getBand(i)*14;
//    if(sig>height*0.8)sig=height*0.8;
//    if(sig<height*0.1)sig=0;
    line(i*1.5f, height, i*1.5f, height - sig);
//reference
    float referencesig = fft.getBand(i)*14;
    line(i*1.5f, height, i*1.5f, height - referencesig);

  }

  
  //fill(255);
  // keep us informed about the window being used
//  text("The window being used is: " + windowName, 5, 20);
}
  
    static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#ece9d8", "mathemusics" });
  }
}
